import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class frmPersonalInfo extends JPanel
{
   JTextField txtName = new JTextField();
   JTextField txtSSN = new JTextField();
   JButton btnCancel = new JButton();
   JButton btnSave = new JButton();
   JLabel lblDOB = new JLabel();
   JTextField txtDOB = new JTextField();
   JTextField txtAddress = new JTextField();
   JTextField txtState = new JTextField();
   JTextField txtCity = new JTextField();
   JTextField txtZip = new JTextField();
   JTextField txtPhone = new JTextField();
   Properties display;
   String CustomerID;
   String PIN;
   JPanel me = this;
   
   /**
    * Default constructor
    */
   public frmPersonalInfo()
   {
      initializePanel();
   }
   
   public frmPersonalInfo(String CustomerID, String PIN, JFrame caller)
   {
	   initializePanel();
	   this.CustomerID = CustomerID;
	   this.PIN = PIN;
	   caller.setVisible(false);
	   caller.dispose();
   }

   /**
    * Adds fill components to empty cells in the first row and first column of the grid.
    * This ensures that the grid spacing will be the same as shown in the designer.
    * @param cols an array of column indices in the first row where fill components should be added.
    * @param rows an array of row indices in the first column where fill components should be added.
    */
   void addFillComponents( Container panel, int[] cols, int[] rows )
   {
      Dimension filler = new Dimension(10,10);

      boolean filled_cell_11 = false;
      CellConstraints cc = new CellConstraints();
      if ( cols.length > 0 && rows.length > 0 )
      {
         if ( cols[0] == 1 && rows[0] == 1 )
         {
            /** add a rigid area  */
            panel.add( Box.createRigidArea( filler ), cc.xy(1,1) );
            filled_cell_11 = true;
         }
      }

      for( int index = 0; index < cols.length; index++ )
      {
         if ( cols[index] == 1 && filled_cell_11 )
         {
            continue;
         }
         panel.add( Box.createRigidArea( filler ), cc.xy(cols[index],1) );
      }

      for( int index = 0; index < rows.length; index++ )
      {
         if ( rows[index] == 1 && filled_cell_11 )
         {
            continue;
         }
         panel.add( Box.createRigidArea( filler ), cc.xy(1,rows[index]) );
      }

   }

   /**
    * Helper method to load an image file from the CLASSPATH
    * @param imageName the package and name of the file to load relative to the CLASSPATH
    * @return an ImageIcon instance with the specified image file
    * @throws IllegalArgumentException if the image resource cannot be loaded.
    */
   public ImageIcon loadImage( String imageName )
   {
      try
      {
         ClassLoader classloader = getClass().getClassLoader();
         java.net.URL url = classloader.getResource( imageName );
         if ( url != null )
         {
            ImageIcon icon = new ImageIcon( url );
            return icon;
         }
      }
      catch( Exception e )
      {
         e.printStackTrace();
      }
      throw new IllegalArgumentException( "Unable to load image: " + imageName );
   }

   public JPanel createPanel()
   {
      JPanel jpanel1 = new JPanel();
      FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:17PX:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:33PX:NONE,FILL:47PX:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE","CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
      CellConstraints cc = new CellConstraints();
      jpanel1.setLayout(formlayout1);

      JLabel jlabel1 = new JLabel();
      jlabel1.setText("Name:");
      jpanel1.add(jlabel1,cc.xy(2,5));

      txtName.setName("txtName");
      jpanel1.add(txtName,cc.xywh(4,5,17,1));

      JLabel jlabel2 = new JLabel();
      jlabel2.setText("SSN:");
      jpanel1.add(jlabel2,cc.xy(2,4));

      txtSSN.setName("txtSSN");
      jpanel1.add(txtSSN,cc.xywh(4,4,7,1));

      JLabel jlabel3 = new JLabel();
      jlabel3.setText("Address:");
      jpanel1.add(jlabel3,cc.xy(2,8));

      JLabel jlabel4 = new JLabel();
      jlabel4.setText("City:");
      jpanel1.add(jlabel4,cc.xy(2,9));

      btnCancel.setActionCommand("Cancel");
      btnCancel.setName("btnCancel");
      btnCancel.setText("Cancel");
      jpanel1.add(btnCancel,cc.xywh(11,13,5,1));

      btnSave.setActionCommand("Ok");
      btnSave.setName("btnSave");
      btnSave.setText("Save");
      jpanel1.add(btnSave,cc.xywh(4,13,6,1));

      JLabel jlabel5 = new JLabel();
      jlabel5.setText("State:");
      jpanel1.add(jlabel5,cc.xywh(9,9,3,1));

      lblDOB.setName("lblDOB");
      lblDOB.setText("DOB:");
      jpanel1.add(lblDOB,cc.xy(12,4));

      txtDOB.setName("txtDOB");
      jpanel1.add(txtDOB,cc.xywh(13,4,8,1));

      txtAddress.setName("txtAddress");
      jpanel1.add(txtAddress,cc.xywh(4,8,17,1));

      txtState.setName("txtState");
      jpanel1.add(txtState,cc.xy(12,9));

      txtCity.setName("txtCity");
      jpanel1.add(txtCity,cc.xywh(4,9,5,1));

      txtZip.setName("txtZip");
      jpanel1.add(txtZip,cc.xywh(15,9,6,1));

      JLabel jlabel6 = new JLabel();
      jlabel6.setText("ZIP:");
      jpanel1.add(jlabel6,cc.xy(14,9));

      JLabel jlabel7 = new JLabel();
      jlabel7.setFont(new Font("Tahoma",Font.BOLD,18));
      jlabel7.setText("Update Personal Information");
      jpanel1.add(jlabel7,cc.xywh(2,2,19,1));

      JLabel jlabel8 = new JLabel();
      jlabel8.setText("Phone:");
      jpanel1.add(jlabel8,cc.xy(2,6));

      txtPhone.setName("txtPhone");
      jpanel1.add(txtPhone,cc.xywh(4,6,7,1));

      addFillComponents(jpanel1,new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 },new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14 });
      
      btnSave.addActionListener( new ActionListener()
	    {
	    	public void actionPerformed( ActionEvent evt )
	    	{
				frmManageCustomer form = new frmManageCustomer(CustomerID, PIN, (JFrame)me.getParent().getParent().getParent().getParent());
		    		JScrollPane scrollable = new JScrollPane(form);
		    		JFrame newFrame = new JFrame();
		            newFrame.add(scrollable);
		            newFrame.setSize(650, 677);
		            newFrame.pack();
		            newFrame.setLocationRelativeTo(null);
		    		newFrame.setVisible(true);
	    	}
	    });
      
      btnCancel.addActionListener( new ActionListener()
	    {
	    	public void actionPerformed( ActionEvent evt )
	    	{
	    		  frmManageCustomer form = new frmManageCustomer(CustomerID, PIN, (JFrame)me.getParent().getParent().getParent().getParent());
		    		JScrollPane scrollable = new JScrollPane(form);
		    		JFrame newFrame = new JFrame();
		            newFrame.add(scrollable);
		            newFrame.setSize(650, 677);
		            newFrame.pack();
		            newFrame.setLocationRelativeTo(null);
		    		newFrame.setVisible(true);
	    	}
	    });
      
      return jpanel1;
   }

   /**
    * Initializer
    */
   protected void initializePanel()
   {
      setLayout(new BorderLayout());
      add(createPanel(), BorderLayout.CENTER);
   }
   
   private void loadFormWithDisplay()
	{
  		txtName.setText(display.getProperty(Constants.NAME));
  		txtDOB.setText(display.getProperty(Constants.DOB));
		txtAddress.setText(display.getProperty(Constants.STREET));
		txtCity.setText(display.getProperty(Constants.CITY));
		txtState.setText(display.getProperty(Constants.STATE));
		txtZip.setText(display.getProperty(Constants.ZIP));
	}
}
